package com.mercenary.wein.screenControl.dao;

import com.mercenary.wein.screenControl.bean.DeviceBean;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by tuxiao on 2017/3/11.
 */
public class DeviceDao {
    Log log = LogFactory.getLog(DeviceDao.class);

    public List<DeviceBean> findBySerialNo(String serialNo){
        String sql = "select serialNo,dname,port,manufacturer,productMode,gcode,status from device t join devicegroup p on t.serialNo = p.did where serialNo =?";
        log.info("find device:"+sql.replace("?",serialNo));
        Connection connection = ConnectDb.Connect();
        QueryRunner queryRunner = new QueryRunner();
        try {
            List<DeviceBean> results = queryRunner.query(connection,sql,
                    new BeanListHandler<DeviceBean>(DeviceBean.class),serialNo);
            DbUtils.close(connection);
            if(results.size()==0)return null;
            return results;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int getMaxPort() {
        String sql = "select max(port) as maxport from device";
        log.info("getMaxPort:" + sql);
        Connection connection = ConnectDb.Connect();
        QueryRunner queryRunner = new QueryRunner();
        try {
            Map<String,Object> results = queryRunner.query(connection, sql, new MapHandler());
            if(results.get("maxport")==null)return 0;
            DbUtils.close(connection);
            return Integer.parseInt(results.get("maxport").toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
        }
        return 0;
    }

    public DeviceBean register(String serialNo,int port,String manufacturer,String productMode ,String dname ,int status){
        String sql = "insert into device values (?,?,?,?,?,?)";
        log.info("register device : "+sql.replaceFirst("\\?",serialNo).replaceFirst("\\?",port+"").replaceFirst("\\?",manufacturer).replaceFirst("\\?",productMode).replaceFirst("\\?",dname));
        Connection connection = ConnectDb.Connect();
        QueryRunner queryRunner = new QueryRunner();
        try {
            queryRunner.update(connection,sql,serialNo,port,manufacturer,productMode,dname,status);
            DbUtils.close(connection);
            return new DeviceBean(serialNo,dname,port,manufacturer,productMode,-1,status);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void resetDeviceStatus(){
        String sql = "update device set status=1";
        log.info("resetDeviceStatus");
        Connection connection = ConnectDb.Connect();
        QueryRunner queryRunner = new QueryRunner();
        try {
            queryRunner.update(connection,sql);
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void setDeviceStatus(String serialNo,int status){
        String sql = "update device set status=? where serialNo=?";
        log.info("setDeviceStatus:status="+status+" serialNo="+serialNo);
        Connection connection = ConnectDb.Connect();
        QueryRunner queryRunner = new QueryRunner();
        try {
            queryRunner.update(connection,sql,status,serialNo);
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void setDeviceName(String serialNo,String dName){
        String sql = "update device set dname=? where serialNo=?";
        log.info("setDeviceStatus:status="+dName+" serialNo="+serialNo);
        Connection connection = ConnectDb.Connect();
        QueryRunner queryRunner = new QueryRunner();
        try {
            queryRunner.update(connection,sql,dName,serialNo);
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
